<?php
namespace api\user\controller;

use api\user\model\UserAddressModel;
use api\common\controller\RestUserBaseController;

class AddressController extends RestUserBaseController
{
    protected $UserAddressModel;

    public function __construct()
    {
        parent::__construct();
        $this->userId = $this->getUserId();
        $this->UserAddressModel = new UserAddressModel();
    }


    public function index()
    {

        $param          = $this->request->param();
        $param['where'] = [
            'user_id' => $this->userId
        ];
        if (!empty($param['type'])) {
            $param['where']['type'] = $param['id'];
        }
        if (!empty($param['id'])) {
            $param['where']['id'] = $param['id'];
        }
        $param['order'] = '-createtime';
        $data = $this->UserAddressModel->getDatas($param);
        $this->success('请求成功', ['list' => $data]);
    }


    public function save()
    {
        $param  = $this->request->param();
        $result = $this->validate($param, 'Address');
        if ($result !== true) {
            $this->error($result);
        }
        $find = $this->UserAddressModel
        ->where(['user_id'=>$this->userId,'type'=>$param['type']])
        ->find();
        if($find){
            $this->error('不能重复添加同类型钱包');
        }
        //组装数据
        $data = $this->_UseraddressObject($param);
        if (!$data) {
            $this->error('添加失败');
        }
        if(isset($data['is_default'])&&$data['is_default']==1){
           $this->UserAddressModel->where(['user_id'=>$this->userId])->update(['is_default' => 0]);
        }
        $res        = $this->UserAddressModel->insert($data);
        $address_id = $this->UserAddressModel->getLastInsID();
        if ($address_id) {
            $this->success('添加成功',['address_id'=>$address_id]);
        } else {
            $this->error('添加失败');
        }

    }

    public function set_default(){
        $param = $this->request->param();
        if (isset($param['id'])) {
            $findAddress = $this->UserAddressModel->where(['user_id'=>$this->userId,'id'=>$param['id']])->find();
            if(!$findAddress){
                $this->error('无效ID');
            }
            $this->UserAddressModel->where(['user_id'=>$this->userId])->update(['is_default' => 0]);
            $result = $this->UserAddressModel->where(['id'=>$param['id']])->update(['is_default' => 1]);
            if ($result) {
                $this->success('设置成功');
            } else {
                $this->error('设置失败');
            }
        }else{
            $this->error('设置失败');
        }
    }

    public function edit()
    {
        if ($this->request->isGet()) { 
            $param   = $this->request->param();
            $address = $this->UserAddressModel->where(['user_id'=>$this->userId,'id'=>$param['id']])->find();
            if($address){
                $this->success('获取成功', $address);
            }else{
               $this->error('无效ID'); 
            }
        }else{
            $param  = $this->request->param();
            $result = $this->validate($param, 'Address');
            if ($result !== true) {
                $this->error($result);
            }
            $address = $this->UserAddressModel->where(['user_id'=>$this->userId,'id'=>$param['id']])->find();
            if(!$address){
                $this->error('无效ID'); 
            }
            //组装数据
            $data = $this->_UseraddressObject($param);
            if (!$data) {
                $this->error('修改失败');
            }
            if(isset($data['is_default'])){
               $this->UserAddressModel->where(['user_id'=>$this->userId])->update(['is_default' => 0]);
            }
            $result = $this->UserAddressModel->allowField(true)->data($data, true)->isUpdate(true)->save();
            if ($result) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }  
        }

    }
    protected function _UseraddressObject($param)
    {
        $data['user_id']        = $this->userId;
        $data['createtime']     = time();
        $data['type']           = $param['type'];
        $data['wallet_address'] = $param['wallet_address'];
        $data['remark']         = isset($param['remark'])?$param['remark']:'';
        $data['is_default']     = isset($param['is_default'])?$param['is_default']:0;
        if(!empty($param['id'])){
          $data['id'] = $param['id'];
        }
        return $data;
    }


    public function delete()
    {
        $param          = $this->request->param();
   
       if (isset($param['id'])) {
                $count = $this->UserAddressModel->where(['id' => $param['id'], 'user_id' => $this->userId])->count();

                if ($count == 0) {
                    $this->error('地址不存在,无法删除');
                }

                $result = $this->UserAddressModel->where(['id' => $param['id']])->delete();
                if($result){
                     $this->success('删除成功');
                }else{
                      $this->error('删除失败'); 
                }
               
        } else{
            $this->error('参数错误');
        }
        
    }
}
